﻿using System;
using System.Collections.Generic;
using System.Data.Objects.DataClasses;

namespace UtfprLibrary.EntityFramework.ComponentModel {
    public class EntityObjectByEntityKeyComparer : IEqualityComparer<EntityObject> {
        public bool Equals(EntityObject x, EntityObject y) {
            //Check whether any of the compared objects is null.
            if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                return false;

            //Check whether the products' properties are equal.
            return x.EntityKey == y.EntityKey;
        }

        public int GetHashCode(EntityObject obj) {
            //Check whether the object is null
            if (Object.ReferenceEquals(obj, null)) return 0;

            //Get hash code for the EntityKey field if it is not null.
            int hashEntityKey = obj.EntityKey == null ? 0 : obj.EntityKey.GetHashCode();

            //Calculate the hash code for the product.
            return hashEntityKey;
        }
    }
}
